<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>交易管理</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
  <div class="d-flex">
    <nav class="flex-column bg-dark text-white p-3" style="width: 220px; height: 100vh;">
      <h4>后台管理</h4>
      <ul class="nav flex-column">
        <li class="nav-item"><a href="/admin/customers" class="nav-link text-white">客户管理</a></li>
        <li class="nav-item"><a href="/admin/cards" class="nav-link text-white">银行卡管理</a></li>
        <li class="nav-item"><a href="/admin/deposits" class="nav-link text-white">存款业务管理</a></li>
        <li class="nav-item"><a href="/admin/transactions" class="nav-link text-white active">交易管理</a></li>
      </ul>
    </nav>
    <main class="flex-fill p-4">
      <h3>交易管理</h3>
      <button class="btn btn-info mb-2">导出CSV</button>
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>交易时间</th>
            <th>交易类型</th>
            <th>交易金额</th>
            <th>卡号</th>
            <th>备注</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody id="transactions-tbody">
        </tbody>
      </table>
    </main>
  </div>
  <script>
  async function loadTransactions() {
    const res = await fetch('/api/admin/trades');
    const data = await res.json();
    const tbody = document.getElementById('transactions-tbody');
    tbody.innerHTML = '';
    data.forEach(t => {
      tbody.innerHTML += `<tr>
        <td>${t.tradeDate ? t.tradeDate : ''}</td>
        <td>${t.tradeType}</td>
        <td>${t.tradeMoney}</td>
        <td>${t.cardID}</td>
        <td>${t.remark}</td>
        <td><button class='btn btn-sm btn-danger' onclick='deleteTrade(${t.id})'>删除</button></td>
      </tr>`;
    });
  }
  async function deleteTrade(id) {
    if (!confirm('确定要删除该交易记录吗？')) return;
    const res = await fetch(`/api/admin/trades/${id}`, { method: 'DELETE' });
    const data = await res.json();
    if (data.msg === '交易已删除') {
      alert('删除成功');
      loadTransactions();
    } else {
      alert(data.msg || '删除失败');
    }
  }
  document.addEventListener('DOMContentLoaded', loadTransactions);
  </script>
  <script src="/static/js/main.js"></script>
</body>
</html>